{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b3ba1cc",
   "metadata": {},
   "source": [
    "# Chat\n",
    "\n",
    "This example will show you how to chat with GPT, the original example is on [OpenAI Example](https://platform.openai.com/docs/guides/chat/introduction), the difference is that we will teach you how to cache the  response for exact and similar matches with **gptcache**, it will be very simple, you just need to add an extra step to initialize the cache.\n",
    "\n",
    "Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`. If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems.\n",
    "\n",
    "Then we can learn the usage and acceleration effect of gptcache by the following code, which consists of three parts, the original openai way, the exact search and the similar search. You can also try this example on [Google Colab](https://colab.research.google.com/drive/1m1s-iTDfLDk-UwUAQ_L8j1C-gzkcr2Sk?usp=share_link)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa0ba70e",
   "metadata": {},
   "source": [
    "## OpenAI API original usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80e9dae2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: what‘s github\n",
      "Time consuming: 6.04s\n",
      "Answer: GitHub is a web-based platform used for version control and collaboration of coding projects. It allows individuals and teams to store, share, and collaborate on changes to code, software, and applications. It also provides features such as issue tracking, project management tools, and code review. It is one of the most popular and widely used online platforms for open-source projects.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import openai\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "\n",
    "question = 'what‘s github'\n",
    "\n",
    "# OpenAI API original usage\n",
    "start_time = time.time()\n",
    "response = openai.ChatCompletion.create(\n",
    "  model='gpt-3.5-turbo',\n",
    "  messages=[\n",
    "    {\n",
    "        'role': 'user',\n",
    "        'content': question\n",
    "    }\n",
    "  ],\n",
    ")\n",
    "print(f'Question: {question}')\n",
    "print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "print(f'Answer: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d871550",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, exact match cache\n",
    "\n",
    "Initalize the cache to run GPTCache and import `openai` form `gptcache.adapter`, which will automatically set the map data manager to match the exact cahe, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache).\n",
    "\n",
    "And if you ask ChatGPT the exact same two questions, the answer to the second question will be obtained from the cache without requesting ChatGPT again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "024484f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "Question: what's github\n",
      "Time consuming: 6.88s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, share, and collaborate on programming projects. It is primarily used for version control, where developers can work on different features and changes of a project simultaneously without overwriting each other's work. GitHub also provides tools for issue tracking, code review, and project management. It is widely used in the open-source community and by software development teams in organizations of all sizes.\n",
      "\n",
      "Question: what's github\n",
      "Time consuming: 0.00s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, share, and collaborate on programming projects. It is primarily used for version control, where developers can work on different features and changes of a project simultaneously without overwriting each other's work. GitHub also provides tools for issue tracking, code review, and project management. It is widely used in the open-source community and by software development teams in organizations of all sizes.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "# To use GPTCache, that's all you need\n",
    "# -------------------------------------------------\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "\n",
    "cache.init()\n",
    "cache.set_openai_key()\n",
    "# -------------------------------------------------\n",
    "\n",
    "question = \"what's github\"\n",
    "for _ in range(2):\n",
    "    start_time = time.time()\n",
    "    response = openai.ChatCompletion.create(\n",
    "      model='gpt-3.5-turbo',\n",
    "      messages=[\n",
    "        {\n",
    "            'role': 'user',\n",
    "            'content': question\n",
    "        }\n",
    "      ],\n",
    "    )\n",
    "    print(f'Question: {question}')\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "    print(f'Answer: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f2ff699",
   "metadata": {},
   "source": [
    "## OpenAI API + GPTCache, similar search cache\n",
    "\n",
    "Set the cache with `embedding_func` to generate embedding for the text, and `data_manager` to manager the cache data, `similarity_evaluation` to evaluate the similarities, more details refer to [build your cache](https://gptcache.readthedocs.io/en/dev/usage.html#build-your-cache).\n",
    "\n",
    "After obtaining an answer from ChatGPT in response to several similar questions, the answers to subsequent questions can be retrieved from the cache without the need to request ChatGPT again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fd1ff06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache loading.....\n",
      "Question: what's github\n",
      "Time consuming: 7.11s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n",
      "Question: can you explain what GitHub is\n",
      "Time consuming: 0.19s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n",
      "Question: can you tell me more about GitHub\n",
      "Time consuming: 0.23s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n",
      "Question: what is the purpose of GitHub\n",
      "Time consuming: 0.21s\n",
      "Answer: GitHub is a web-based platform that allows developers to store, manage, review, and collaborate on code repositories. It is a version control system that enables developers to track changes they make in code over time and collaborate on projects with other developers. GitHub is used by millions of developers worldwide to share code, collaborate on open-source projects, and contribute to projects owned by others. It's also a hub for various communities and forums related to software development.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def response_text(openai_resp):\n",
    "    return openai_resp['choices'][0]['message']['content']\n",
    "\n",
    "from gptcache import cache\n",
    "from gptcache.adapter import openai\n",
    "from gptcache.embedding import Onnx\n",
    "from gptcache.manager import CacheBase, VectorBase, get_data_manager\n",
    "from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation\n",
    "\n",
    "print(\"Cache loading.....\")\n",
    "\n",
    "onnx = Onnx()\n",
    "data_manager = get_data_manager(CacheBase(\"sqlite\"), VectorBase(\"faiss\", dimension=onnx.dimension))\n",
    "cache.init(\n",
    "    embedding_func=onnx.to_embeddings,\n",
    "    data_manager=data_manager,\n",
    "    similarity_evaluation=SearchDistanceEvaluation(),\n",
    "    )\n",
    "cache.set_openai_key()\n",
    "\n",
    "questions = [\n",
    "    \"what's github\",\n",
    "    \"can you explain what GitHub is\",\n",
    "    \"can you tell me more about GitHub\",\n",
    "    \"what is the purpose of GitHub\"\n",
    "]\n",
    "\n",
    "for question in questions:\n",
    "    start_time = time.time()\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model='gpt-3.5-turbo',\n",
    "        messages=[\n",
    "            {\n",
    "                'role': 'user',\n",
    "                'content': question\n",
    "            }\n",
    "        ],\n",
    "    )\n",
    "    print(f'Question: {question}')\n",
    "    print(\"Time consuming: {:.2f}s\".format(time.time() - start_time))\n",
    "    print(f'Answer: {response_text(response)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07d92eae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
